#include "menu.h"
#include "input.h"

Menu_t menu; // 全局结构体

Page_t root_page;
Page_t item1_page;
Page_t item11_page;
Page_t item2_page;
// menu_page_t item2_page;
// menu_page_t item3_page;
// menu_page_t item4_page;
// menu_page_t item5_page;
Item_t root_page_item[10];
Item_t item1_page_item[5];
Item_t item2_page_item[5];
Item_t item11_page_item[5];

uint8_t checkbox1_flag = 0;
uint8_t checkbox2_flag = 0;
uint8_t checkbox3_flag = 0;
uint8_t switch1_flag = 0;
uint8_t switch2_flag = 0;
uint8_t switch3_flag = 0;

void menu_init(void)
{   
    menu_add_page(&root_page,"[root page]",sizeof("[root page]")); // 初始化根页面
    menu_add_page(&item1_page,"[item1 page]",sizeof("[item1 page]")); 
    menu_add_page(&item2_page,"[item2 page]",sizeof("[item2 page]")); 
    menu_add_page(&item11_page,"[item11 page]",sizeof("[item11 page]")); 
    // menu_page_init(&item3_page);
    // menu_page_init(&item4_page);
    // menu_page_init(&item5_page);   

    // 初始化列表
    menu_add_item(&root_page,&root_page_item[0],"Test checkbox",sizeof("Test checkbox"),ITEM_TREE,&item1_page,NULL,NULL);
    menu_add_item(&root_page,&root_page_item[1],"item2",sizeof("item2"),ITEM_TREE,&item2_page,NULL,NULL);
    menu_add_item(&root_page,&root_page_item[2],"Test radio button",sizeof("Test radio button"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&root_page,&root_page_item[3],"checkbox1",sizeof("checkbox1"),ITEM_CHECKBOX,NULL,NULL,&checkbox1_flag);
    menu_add_item(&root_page,&root_page_item[4],"checkbox2",sizeof("checkbox2"),ITEM_CHECKBOX,NULL,NULL,&checkbox2_flag);
    menu_add_item(&root_page,&root_page_item[5],"checkbox3",sizeof("checkbox3"),ITEM_CHECKBOX,NULL,NULL,&checkbox3_flag);
    menu_add_item(&root_page,&root_page_item[6],"switch1",sizeof("switch1"),ITEM_SWITCH,NULL,NULL,&switch1_flag);
    menu_add_item(&root_page,&root_page_item[7],"switch2",sizeof("switch2"),ITEM_SWITCH,NULL,NULL,&switch2_flag);
    menu_add_item(&root_page,&root_page_item[8],"switch3",sizeof("switch3"),ITEM_SWITCH,NULL,NULL,&switch3_flag);
    menu_add_item(&root_page,&root_page_item[9],"item10",sizeof("item10"),ITEM_NO,NULL,NULL,NULL);

    // 将列表插入到item1页面
    menu_add_item(&item1_page,&item1_page_item[0],"item11",sizeof("item11"),ITEM_TREE,&item11_page,NULL,NULL);
    menu_add_item(&item1_page,&item1_page_item[1],"item12",sizeof("item12"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item1_page,&item1_page_item[2],"item13",sizeof("item13"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item1_page,&item1_page_item[3],"item14",sizeof("item14"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item1_page,&item1_page_item[4],"item15",sizeof("item15"),ITEM_NO,NULL,NULL,NULL);

    // 将列表插入到item11页面
    menu_add_item(&item11_page,&item11_page_item[0],"item111",sizeof("item111"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item11_page,&item11_page_item[1],"item112",sizeof("item112"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item11_page,&item11_page_item[2],"item113",sizeof("item113"),ITEM_NO,NULL,NULL,NULL);

    // 将列表插入到item2页面
    menu_add_item(&item2_page,&item2_page_item[0],"item21",sizeof("item21"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item2_page,&item2_page_item[1],"item22",sizeof("item22"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item2_page,&item2_page_item[2],"item23",sizeof("item23"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item2_page,&item2_page_item[3],"item24",sizeof("item24"),ITEM_NO,NULL,NULL,NULL);
    menu_add_item(&item2_page,&item2_page_item[4],"item25",sizeof("item25"),ITEM_NO,NULL,NULL,NULL);

    menu.current_page = &root_page; // 当前页面指向根界面
    menu.current_item = root_page.item_head; // 当前指向第一页的第一项
    menu.page_refresh = 1; // 页面刷新
    menu.menu_state = UI_PAGE_INIT;

    menu.camera.camera_pos = 1;
    menu.cursor.nowX = 0;
    menu.cursor.nowY = 0;
    menu.cursor.cursor_ani.kp = 0.5f;
    menu.cursor.cursor_ani.ki = 0.1f;
    menu.cursor.cursor_ani.kd = 0.1f;


}

/**
 * 菜单页面初始化
 */
void menu_add_page(Page_t * menu_page,char *title,uint8_t title_size)
{
    if(title_size >= TITLE_LEN)
        title_size = TITLE_LEN;
    // 拷贝字符串
    memcpy(menu_page->title,title,title_size);

    menu_page->item_sum = 0;                // 页面列表总数初始化为0
    menu_page->cursor_pos = 1;
    menu_page->item_head = NULL;
    menu_page->item_tail = NULL;
    menu_page->parent_item = NULL;          // 页面的父列表项
}

/**
 * 菜单列表项初始化
 */
void menu_add_item(Page_t * menu_page,Item_t * menu_item,char *title,uint8_t title_size,ItemType_e item_type,Page_t * child_page,Callback_t callback,uint8_t *flag)
{   
    if(title_size >= TITLE_LEN)
        title_size = TITLE_LEN;
    // 拷贝列表项字符串
    memcpy(menu_item->title,title,title_size);
    // 列表项类型
    menu_item->item_type = item_type;
    // 列表项所属页面
    menu_item->local_page = NULL;
    // 列表项 若为目录 则子界面
    if(menu_item->item_type == ITEM_TREE)
    {
        menu_item->child_page = child_page;
        child_page->parent_item = menu_item;
    }
    // 列表项若为执行项 则定义回调函数
    else if(menu_item->item_type == ITEM_BLOB)
    {
        menu_item->callback = callback;
    }
    else if(menu_item->item_type == ITEM_SWITCH || menu_item->item_type == ITEM_CHECKBOX)
    {
        menu_item->flag = flag;
    }
    // 双向链表清0
    menu_item->preItem = NULL;
    menu_item->nextItem = NULL;
    
    // 插入到页面 
    menu_page->item_sum++;
    menu_item->pos = menu_page->item_sum;
    menu_item->local_page = menu_page;
    
    // 第一次插入
    if(menu_page->item_head == NULL)
    {
        menu_page->item_head = menu_item;
        menu_page->item_tail = menu_item;
    }
    // 将列表项插到最后面
    else
    {
        menu_item->preItem = menu_page->item_tail;
        menu_page->item_tail->nextItem = menu_item;
        menu_page->item_tail = menu_page->item_tail->nextItem;
    }

    
}


static Item_t * find_item(Page_t * menu_page)
{
    uint8_t index = menu_page->cursor_pos;
    Item_t * pItem = menu_page->item_head;
    while(index != 1)
    {
        pItem = pItem->nextItem;
        index--;
    }
    return pItem;
    
}



// 输入处理
void menu_input_handle(input_e input)
{
    // 初始化
    if(UI_PAGE_INIT == menu.menu_state)
    {
        if(INPUT_ENTER == input)
        {
            menu.menu_state = UI_PAGE_DRAWING;
        }
    }
    else if(UI_PAGE_RUNING == menu.menu_state || UI_PAGE_DRAWING == menu.menu_state)
    {
        switch (input)
        {
            case INPUT_UP:
                if(menu.current_page->cursor_pos > 1)
                {
                    menu.current_page->cursor_pos--;
                    menu.current_item = menu.current_item->preItem;
                    if(menu.current_page->cursor_pos < menu.camera.camera_pos)
                    {
                        menu.camera.camera_pos--;
                    }
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                else
                {
                    menu.current_page->cursor_pos = menu.current_page->item_sum;
                    menu.current_item = menu.current_page->item_tail;
                    if(menu.current_page->cursor_pos > 3)
                    {
                        menu.camera.camera_pos = 1 + menu.current_page->cursor_pos - 3;
                    }
                    menu.menu_state = UI_PAGE_DRAWING;

                }
                break;
            case INPUT_DOWN:
                if(menu.current_page->cursor_pos < menu.current_page->item_sum)
                {
                    menu.current_page->cursor_pos++; 
                    menu.current_item = menu.current_item->nextItem;
                    if(menu.current_page->cursor_pos > 3)
                    {
                        menu.camera.camera_pos = 1 + menu.current_page->cursor_pos - 3;
                    }
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                else
                {
                    menu.current_page->cursor_pos = 1;
                    menu.current_item = menu.current_page->item_head;
                    menu.camera.camera_pos = 1;
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                break;
            case INPUT_ENTER:
                if(ITEM_NO == menu.current_item->item_type)
                {

                }
                else if(ITEM_TREE == menu.current_item->item_type)
                {
                    menu.current_page = menu.current_item->child_page;
                    menu.current_item->child_page->cursor_pos = 1;
                    menu.current_item = menu.current_item->child_page->item_head;
                    menu.camera.camera_pos = 1;
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                else if(ITEM_BLOB == menu.current_item->item_type) 
                {
                    // 
                }
                else if(ITEM_CHECKBOX == menu.current_item->item_type || ITEM_SWITCH == menu.current_item->item_type) 
                {
                    *menu.current_item->flag = !(*menu.current_item->flag);
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                
                break;
            case INPUT_EXIT:
                if(menu.current_page->parent_item != NULL)
                {
                    menu.current_page = menu.current_page->parent_item->local_page;
                    menu.current_item = find_item(menu.current_page);
                    menu.menu_state = UI_PAGE_DRAWING;
                }
                break;
            default:
                break;
        }
    }

}


float UI_Animation(float targrt, float now, ui_animation_param_t *obj)
{
    float x = now;
    
    // 计算误差
    obj->error = targrt - x;
    // 积分环节，累加误差
    obj->sum_error += obj->error;
    // 微分环节，计算误差变化率
    float delta_error = obj->error - obj->last_error;

    // 计算控制量
    float velocity = obj->kp * obj->error + obj->ki * obj->sum_error + obj->kd * delta_error;
    
    // 更新状态
    x += velocity;
    obj->last_error = obj->error;

    return x;
}


void menu_page_refresh(void)
{
    Item_t * start_item;
    uint8_t camera_index = 0;
    // 初始化图像
    if(UI_PAGE_INIT == menu.menu_state)
    {

    }
    else if(UI_PAGE_DRAWING == menu.menu_state)
    {
        camera_index = menu.camera.camera_pos;

        
        OLED_Clear();
        // 绘制界面标题
        OLED_ShowString(0,3,menu.current_page->title,OLED_6X8);

        start_item = menu.current_page->item_head;
        while(camera_index != 1)
        {
            camera_index--;
            start_item = start_item->nextItem;
        }
        // 打印列表项
        for(uint8_t i = 1;i < 4; i++)
        {
            if(start_item!=NULL)
            {
                // 绘制字符
                OLED_ShowString(0,3+16*i,start_item->title,OLED_6X8);
                // 复选框
                if(start_item->item_type == ITEM_CHECKBOX)
                {   
                    OLED_DrawRectangle(128-20,2+16*i,10,10,0);
                    if(*start_item->flag)
                    {
                        OLED_DrawRectangle(128-18,4+16*i,6,6,1);
                    }
                }
                else if(start_item->item_type == ITEM_SWITCH)
                {
                    if(*start_item->flag)
                    {
                        OLED_ShowString(128-25,2+16*i,"ON",OLED_6X8);
                    }
                    else
                    {
                        OLED_ShowString(128-25,2+16*i,"OFF",OLED_6X8);
                    }
                    
                }
                start_item = start_item->nextItem;

            }
        }

        // 光标 需要异或模式
        OLED_SetDrawColor(XOR);
        OLED_DrawRectangle(menu.cursor.nowX,menu.cursor.nowY-2,menu.cursor.nowW,12,1);
        OLED_SetDrawColor(NORMAL);



        // 右边进度条
        uint8_t y_len = menu.current_page->cursor_pos * 48 /  menu.current_page->item_sum;
        OLED_DrawRectangle(123,16,4,y_len,1);
        OLED_Update();

        // 光标绘制完成后
        if(menu.cursor.nowX == menu.cursor.targetX && 
            menu.cursor.nowY == menu.cursor.targetY && 
            menu.cursor.nowW == menu.cursor.targetW && 
            menu.cursor.cursor_ani.error == 0)
        {
            menu.menu_state = UI_PAGE_RUNING;
        }
        else
        {
            menu.cursor.nowY = (uint8_t)UI_Animation((float)menu.cursor.targetY,(float)menu.cursor.nowY, &menu.cursor.cursor_ani);
            menu.cursor.nowW = (uint8_t)UI_Animation((float)menu.cursor.targetW,(float)menu.cursor.nowW, &menu.cursor.cursor_ani);

        }

    }




}


void menu_get_menu_info(void)
{
    uint8_t target_pos = 0;

    target_pos = menu.current_page->cursor_pos - menu.camera.camera_pos;
    // 获得目标XY
    menu.cursor.targetX = 0;
    menu.cursor.targetY = 16 + 3 + target_pos*16;
    // 获得长度
    menu.cursor.targetW = 2+strlen(menu.current_item->title)*6;

}

void  menu_loop(void)
{
    
    input_e input;
    // 无动画
    // 响应按键动作 然后绘图
    input = read_input();

    menu_input_handle(input); // 输入处理

    menu_get_menu_info();

    menu_page_refresh(); 




}





